package aiyh.utils.service;

import aiyh.utils.dao.UtilDao;
import aiyh.utils.entity.ApiConfigDetailDTO;
import aiyh.utils.entity.ApiConfigMainDTO;
import aiyh.utils.entity.MultiLanguageDTO;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author EBU7-dev1-ayh
 * @create 2021/10/14 0014 12:21
 * service类
 */


public class UtilService {
	private final UtilDao utilDao = new UtilDao();

	public ApiConfigMainDTO getApiConfigMain(String id){
		ApiConfigMainDTO apiConfigMain = utilDao.getApiConfigMain(id);
		List<ApiConfigDetailDTO> apiConfigDetails = utilDao.getApiConfigDetail(apiConfigMain.getId());
		apiConfigMain.setDetails(apiConfigDetails);
		return apiConfigMain;
	}

	public ApiConfigMainDTO getApiConfigMainTree(String id){
		ApiConfigMainDTO apiConfigMain = utilDao.getApiConfigMain(id);
		List<ApiConfigDetailDTO> apiConfigDetails = utilDao.getApiConfigDetail(apiConfigMain.getId());
		List<ApiConfigDetailDTO> apiConfigDetailTreeList = list2Tree(apiConfigDetails);
		apiConfigMain.setDetails(apiConfigDetailTreeList);
		return apiConfigMain;
	}

	private List<ApiConfigDetailDTO> list2Tree(List<ApiConfigDetailDTO> list) {
		List<ApiConfigDetailDTO> treeList = new ArrayList<>();
		for (ApiConfigDetailDTO apiConfigDetailDTO : list) {
			if (apiConfigDetailDTO.getObjectChild() == 0) {
				ApiConfigDetailDTO child = findChild(list, apiConfigDetailDTO);
				treeList.add(child);
			}
		}
		return treeList;
	}

	private ApiConfigDetailDTO findChild(List<ApiConfigDetailDTO> list, ApiConfigDetailDTO apiConfigDetailDTO) {
		if (apiConfigDetailDTO.getParamType() != 3) {
			return apiConfigDetailDTO;
		}
		for (ApiConfigDetailDTO ddConfigDetailDTO : list) {
			if (ddConfigDetailDTO.getParentLine() == apiConfigDetailDTO.getLineNum()) {
//				子项
				if (apiConfigDetailDTO.getChildren() == null) {
					apiConfigDetailDTO.setChildren(new ArrayList<>());
				}
				apiConfigDetailDTO.getChildren().add(findChild(list, ddConfigDetailDTO));
			}
		}
		return apiConfigDetailDTO;
	}

	public Map<String, String> queryLanguage(int groupId, int languageId){
		List<MultiLanguageDTO> multiLanguageDTOS = utilDao.queryLanguage(groupId);
		Map<String, String> languageResult = new HashMap<>();
		if(languageId == 9){
//			中文繁体
			for (MultiLanguageDTO multiLanguageDTO : multiLanguageDTOS) {
				languageResult.put(multiLanguageDTO.getBswyz(),multiLanguageDTO.getZnHk());
			}
		}else if(languageId == 8){
//			英文
			for (MultiLanguageDTO multiLanguageDTO : multiLanguageDTOS) {
				languageResult.put(multiLanguageDTO.getBswyz(),multiLanguageDTO.getEnUs());
			}
		}else {
//			中文简体
			for (MultiLanguageDTO multiLanguageDTO : multiLanguageDTOS) {
				languageResult.put(multiLanguageDTO.getBswyz(),multiLanguageDTO.getZnCh());
			}
		}
		return languageResult;
	}
}
